<?php

namespace App\Http\Controllers\Home;

use App\Model\writer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class userHomeController extends Controller
{
    public function show()
    {

        $icons = writer::where('id',session('writerid'))->get();
        foreach($icons as $k){
            $icon = $k->icon;
        }
        $lis = DB::table('writer_news')->paginate(10);
        return view('huweize.layouts.userHome',compact('lis','icon'));
    }

    public function out(Request $request,$id)
    {
        $request->session()->forget('writerid');
        $request->session()->forget('nickname');
        return redirect('zjxhome/index');
    }

    public function pass(Request $request,$id)
    {
        $pwd = writer::find($id);
        $pwd->password = $request->password;
        $pwd->save();
    }

    public function feiq(Request $request)
    {
//        return $request->qq;
        //配置您申请的appkey
        $appkey = "1878da48d000ab67ed7f5334f8070f83";
        //************1.QQ号码测吉凶 ************
        $url = "http://japi.juhe.cn/qqevaluate/qq";
        $params = array(
            "key" => $appkey,//您申请的appKey
            "qq" => $request->qq,//需要测试的QQ号码
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);

        $result = json_decode($content,true);
        if($result){
            if($result['error_code']=='0'){
                return $result;
//                return (json_encode($result));
            }else{
                echo $result['error_code'].":".$result['reason'];
            }
        }else{
            echo "请求失败";
        }

    }

    public function qq()
    {
        return view('huweize.home.feiq');
    }
    public function juhecurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }
}
